package com.sojson.project.controller.param.verify;

import java.sql.SQLException;
import java.util.Date;

import com.sojson.enums.EPostType;
import com.sojson.enums.core.EGeneralYesNo;
import com.sojson.enums.core.ESex;
import com.sojson.project.entity.po.Post;
import com.sojson.result.Result;
import com.sojson.util.MatcheUtil;
import com.sojson.util.StringUtil;

/**
 * 职位表参数验证
 * 
 * @author liu
 * @date 2021-03-16
 */
public class PostParamVerify {

    /**
     * 单条插入
     * 
     * @param post
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("deprecation")
    public static Result<?> insert(Post post) {
        Integer type = post.getType();
        if (StringUtil.isBlank(type)) {
            return Result.fail("职位类型不能为空！");
        }
        if (StringUtil.isBlank(post.getTypeDescId())) {
            return Result.fail("职位详细类型不能为空！");
        }
        String name = post.getName();
        if (StringUtil.isBlank(name)) {
            return Result.fail("职位名称不能为空！");
        }
        int lenName = name.length();
        if (lenName > 20) {
            return Result.fail("职位名称不能超过20个字符！");
        }
        Float targetX = post.getTargetX();
        if (StringUtil.isBlank(targetX)) {
            return Result.fail("经度不能为空！");
        }
        Float targetY = post.getTargetY();
        if (StringUtil.isBlank(targetY)) {
            return Result.fail("维度不能为空！");
        }
        if (targetX > 180 || targetX <= -180 || targetY > 90 || targetY < -90) {
            return Result.fail("经纬度值错误！");
        }
        if (StringUtil.isBlank(post.getCity())) {
            return Result.fail("市不能为空！");
        }
        if (StringUtil.isBlank(post.getArea())) {
            return Result.fail("区不能为空！");
        }
        if (StringUtil.isBlank(post.getAddress())) {
            return Result.fail("详细地址不能为空！");
        }
        if (StringUtil.isBlank(post.getNumPeople())) {
            return Result.fail("人数不能为空！");
        }
        String mobile = post.getMobile();
        if (StringUtil.isBlank(mobile)) {
            return Result.fail("手机号不能为空！");
        }
        if (!MatcheUtil.isMobile(mobile)) {
            return Result.fail("手机号格式错误！");
        }
        if (StringUtil.isBlank(post.getWx())) {
            return Result.fail("微信号不能为空！");
        }
        if (StringUtil.isBlank(post.getSettleType())) {
            return Result.fail("结算类型不能为空！");
        }
        if (StringUtil.isBlank(post.getImg())) {
            return Result.fail("详情图片不能为空！");
        }
        if (StringUtil.isBlank(post.getImgs())) {
            return Result.fail("列表图片不能为空！");
        }
        if (StringUtil.isBlank(post.getSexType())) {
            post.setSexType(ESex.UNKNOWN.getCode());
        }
        if (StringUtil.isBlank(post.getIsRem())) {
            post.setIsRem(EGeneralYesNo.NO.getCode());
        }
        int shortCode = EPostType.SHORT.getCode();
        int longCode = EPostType.LONG.getCode();
        if (type == shortCode || type == longCode) {
            Date startYear = post.getStartYear();
            if (StringUtil.isBlank(startYear)) {
                return Result.fail("工作开始年份不能为空！");
            }
            if (StringUtil.isBlank(post.getIsDeposit())) {
                post.setIsDeposit(EGeneralYesNo.NO.getCode());
            }
            if (StringUtil.isBlank(post.getPayHour())) {
                return Result.fail("薪资(/小时)不能为空！");
            }
            Date startTime = post.getStartTime();
            if (StringUtil.isBlank(startTime)) {
                return Result.fail("工作开始时间不能为空！");
            }
            Date endTime = post.getEndTime();
            if (StringUtil.isBlank(post.getEndTime())) {
                return Result.fail("工作结束时间不能为空！");
            }
            Date startDate = new Date(startYear.getTime());
            Date endDate = new Date(startYear.getTime());
            if (EPostType.LONG.getCode() == type) {
                endDate = new Date(post.getEndYear().getTime());
                if (StringUtil.isBlank(post.getEndYear())) {
                    return Result.fail("工作结束年份不能为空！");
                }
            }
            startDate.setHours(startTime.getHours());
            startDate.setMinutes(startTime.getMinutes());
            startDate.setSeconds(startTime.getSeconds());
            endDate.setHours(endTime.getHours());
            endDate.setMinutes(endTime.getMinutes());
            endDate.setSeconds(endTime.getSeconds());
            if (startDate.getTime() >= endDate.getTime()) {
                return Result.fail("开始时间必须小于结束时间！");
            }
            post.setCloseTimeReady(endDate);
            post.setPaySort(post.getPayHour());
        } else if (EPostType.FULL_TIME.getCode() == type) {
            if (StringUtil.isBlank(post.getCompanyName())) {
                return Result.fail("公司名称不能为空！");
            }
            if (StringUtil.isBlank(post.getCompanyIntroduce())) {
                return Result.fail("公司介绍不能为空！");
            }
            if (StringUtil.isBlank(post.getPayHeight())) {
                return Result.fail("薪资(/月)最高不能为空！");
            }
            if (StringUtil.isBlank(post.getPayLow())) {
                return Result.fail("薪资(/月)最低不能为空！");
            }
            if (StringUtil.isBlank(post.getUndergo())) {
                return Result.fail("工作经验不能为空！");
            }
            String welfare = post.getWelfare();
            if (StringUtil.isNotBlank(welfare)) {
                String[] split = welfare.split(",");
                for (String string : split) {
                    if (string.length() > 4) {
                        return Result.fail("每个工作福利不能超过4个字！");
                    }
                }
            }
            post.setPaySort(post.getPayLow());
        } else {
            return Result.fail("职位类型不存在！");
        }

        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param post
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("deprecation")
    public static Result<?> updatePartialById(Post post) {
        if (StringUtil.isBlank(post.getId())) {
            return Result.fail("ID不能为空！");
        }
        Float targetX = post.getTargetX();
        if (StringUtil.isNotBlank(targetX)) {
            if (targetX > 180 || targetX <= -180) {
                return Result.fail("经度值错误！");
            }
        }
        Float targetY = post.getTargetY();
        if (StringUtil.isNotBlank(targetY)) {
            if (targetY > 90 || targetY < -90) {
                return Result.fail("纬度值错误！");
            }
        }
        Integer type = post.getType();
        if (StringUtil.isBlank(type)) {
            return Result.fail("职位类型不能为空！");
        }
        int shortCode = EPostType.SHORT.getCode();
        int longCode = EPostType.LONG.getCode();
        if (type == shortCode || type == longCode) {
            Date startYear = post.getStartYear();
            if (StringUtil.isBlank(startYear)) {
                return Result.fail("工作开始年份不能为空！");
            }
            if (StringUtil.isBlank(post.getIsDeposit())) {
                post.setIsDeposit(EGeneralYesNo.NO.getCode());
            }
            if (StringUtil.isBlank(post.getPayHour())) {
                return Result.fail("薪资(/小时)不能为空！");
            }
            Date startTime = post.getStartTime();
            if (StringUtil.isBlank(startTime)) {
                return Result.fail("工作开始时间不能为空！");
            }
            Date endTime = post.getEndTime();
            if (StringUtil.isBlank(post.getEndTime())) {
                return Result.fail("工作结束时间不能为空！");
            }
            Date startDate = new Date(startYear.getTime());
            Date endDate = new Date(startYear.getTime());
            if (EPostType.LONG.getCode() == type) {
                endDate = new Date(post.getEndYear().getTime());
                if (StringUtil.isBlank(post.getEndYear())) {
                    return Result.fail("工作结束年份不能为空！");
                }
            }
            startDate.setHours(startTime.getHours());
            startDate.setMinutes(startTime.getMinutes());
            startDate.setSeconds(startTime.getSeconds());
            endDate.setHours(endTime.getHours());
            endDate.setMinutes(endTime.getMinutes());
            endDate.setSeconds(endTime.getSeconds());
            if (startDate.getTime() >= endDate.getTime()) {
                return Result.fail("开始时间必须小于结束时间！");
            }
            post.setCloseTimeReady(endDate);
            post.setPaySort(post.getPayHour());
        } else if (EPostType.FULL_TIME.getCode() == type) {
            if (StringUtil.isBlank(post.getCompanyName())) {
                return Result.fail("公司名称不能为空！");
            }
            if (StringUtil.isBlank(post.getCompanyIntroduce())) {
                return Result.fail("公司介绍不能为空！");
            }
            if (StringUtil.isBlank(post.getPayHeight())) {
                return Result.fail("薪资(/月)最高不能为空！");
            }
            if (StringUtil.isBlank(post.getPayLow())) {
                return Result.fail("薪资(/月)最低不能为空！");
            }
            if (StringUtil.isBlank(post.getUndergo())) {
                return Result.fail("工作经验不能为空！");
            }
            String welfare = post.getWelfare();
            if (StringUtil.isBlank(welfare)) {
                return Result.fail("工作福利不能为空！");
            }
            String[] split = welfare.split(",");
            for (String string : split) {
                if (string.length() > 4) {
                    return Result.fail("每个工作福利不能超过4个字！");
                }
            }
            post.setPaySort(post.getPayLow());
        } else {
            return Result.fail("职位类型不存在！");
        }

        return Result.success();
    }

    /**
     * 通过id修改状态
     * 
     * @param post
     * @return
     * @throws SQLException
     */
    public static Result<?> updateStatus(Post post) {
        if (StringUtil.isBlank(post.getId())) {
            return Result.fail("ID不能为空！");
        }

        return Result.success();
    }

}